package com.ibeeking.found.open.b.rest.controller.v1;

import com.ibeeking.found.common.config.secret.RsaConfig;
import com.ibeeking.found.open.service.common.param.RsaCheckParam;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.encryption.EncryptUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @ClassName ParkingController控制器
 * @Description
 * @Author ibeeking
 * @Date 2021-06-02 18:15
 **/
@RestController
@RequestMapping("/v1/open/api/rsa")
public class RsaTestController {

    @Resource
    private RsaConfig rsaConfig;

    @Value("${spring.profiles.active}")
    private String env;

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "RSA校验")
    @PostMapping(value = "/rsaPubCheck")
    public String check(@RequestBody @Validated RsaCheckParam param) {
        if ("prod".equalsIgnoreCase(env)) {
            return "";
        }
        return EncryptUtils.RsaEncrypt.decrypt(param.getPublicKey(), rsaConfig.getKeyPair().getPrivate());
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "RSA公钥加密")
    @PostMapping(value = "/rsaPubPwd")
    public String rsaPubPwd(@RequestBody @Validated RsaCheckParam param) {
        if ("prod".equalsIgnoreCase(env)) {
            return "";
        }
        return EncryptUtils.RsaEncrypt.encrypt(param.getPublicKey(), rsaConfig.getKeyPair().getPublic());
    }
}
